package org.mclll.xwangplugin.plant.config;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.bukkit.Material;
import org.bukkit.block.Biome;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author: geduo
 * @date: 2023/9/19 22:41
 */
public class PlantConfig {

    public static final String CONFIG_FILE_NAME = "plantGrow.yml";

    public static Boolean debugMode;
    private static Map<Biome, List<PlantGrowControl>> plantGrowControlData;
    public static void reloadConfig(File configFolder) {
        if (plantGrowControlData == null) {
            plantGrowControlData = Maps.newHashMap();
        } else {
            plantGrowControlData.clear();
        }
        YamlConfiguration config = YamlConfiguration.loadConfiguration(new File(configFolder, CONFIG_FILE_NAME));
        debugMode = config.getBoolean("Debug");
        ConfigurationSection biomeList = config.getConfigurationSection("Biome");
        Set<String> biomeNameList = biomeList.getKeys(false);
        for (String biomeName : biomeNameList) {
            Biome biome;
            try {
                biome = Biome.valueOf(biomeName);
            } catch (IllegalArgumentException e) {
                continue;
            }
            List<PlantGrowControl> resultList = Lists.newLinkedList();
            ConfigurationSection planList = biomeList.getConfigurationSection(biomeName);
            Set<String> planNameList = planList.getKeys(false);
            for (String plantMaterialName : planNameList) {
                Material plantMaterial = Material.valueOf(plantMaterialName);
                double probability = planList.getDouble(plantMaterialName);
                double totalAge = PlantConfig.getTotalAge(plantMaterial);
                double realProbability = Math.pow(probability, 1.0 / totalAge);
                resultList.add(new PlantGrowControl(plantMaterial, realProbability));
            }
            plantGrowControlData.put(biome, resultList);
        }
    }

    public static PlantGrowControl get(Biome biome, Material plantType) {
        List<PlantGrowControl> plantGrowControlList = plantGrowControlData.get(biome);
        if (plantGrowControlList == null) {
            return null;
        }
        for (PlantGrowControl plantGrowControl : plantGrowControlList) {
            if (plantGrowControl.getPlant().equals(plantType)) {
                return plantGrowControl;
            }
        }
        return null;
    }

    public static class PlantGrowControl {
        private Material plant;
        private Double probability;

        public PlantGrowControl() {
        }

        public PlantGrowControl(Material plant, Double probability) {
            this.plant = plant;
            this.probability = probability;
        }

        public Material getPlant() {
            return plant;
        }

        public void setPlant(Material plant) {
            this.plant = plant;
        }

        public Double getProbability() {
            return probability;
        }

        public void setProbability(Double probability) {
            this.probability = probability;
        }
    }

    public static double getTotalAge(Material plantType) {
        switch (plantType) {
            case BEETROOT:
            case NETHER_WART:
            case COCOA:
            case SWEET_BERRY_BUSH:
                return 3.0;
            case SUGAR_CANE:
            case CACTUS:
                return 2.0;
            case MELON:
            case PUMPKIN:
            case BAMBOO_SAPLING:
            case KELP:
                return 1.0;
            case BAMBOO:
                return 14.0;
            default:
                return 7.0;
        }
    }
}
